DB dbtest

I will really appreciate a little help on how to replace space or any character from a string or any type of text.

Example below.

Thanks

DB dbtest 
DBE fld, fld2, btn
 
 
void replaceText (DBE btn)
{
 
//I need to create a function to replace or recognize the spaces in the Field box for the "|". 
//This is like a search engine I want to create and
 
 
string s = get(fld)
string nTxt = "Man|Woman|Child" //Desired Text
set(fld, nTxt)
 
}
 
 
dbtest = create("Test DB", styleCentered)
fld = field(dbtest, "Enter Text: ", "Man Woman Child", 30, false)
right(dbtest)
btn = button(dbtest, "Replace", replaceText)
 
setFocus(btn)
 
show(dbtest)

SystemAdmin - Tue Dec 15 14:26:00 EST 2009

Re: DB dbtest
ePiallat - Thu Dec 17 05:32:39 EST 2009

Hi,

As you only need to replace a single character by another single character, I can't imagine simplier than that:

DB dbtest 
DBE fld, fld2, btn
 
 
char OLDCHAR = ' '
char NEWCHAR = '|'
 
void replaceText (DBE btn)
{
  Buffer b = create 
  b = (string get fld)
  int i
  for i in 1:length b do if b[i] == OLDCHAR then set (b,i,NEWCHAR)
  set(fld, stringOf b)
  delete b 
}
 
dbtest = create("Test DB", styleCentered)
fld = field(dbtest, "Enter Text: ", "Man Woman Child", 30, false)
right(dbtest)
btn = button(dbtest, "Replace", replaceText)
 
setFocus(btn)
 
show(dbtest)


Of course, it would be more complicated if you wanted to replace one character by two, say "/" by "\/" for instance...

- Éric

Re: DB dbtest
llandale - Thu Dec 17 13:22:16 EST 2009

ePiallat - Thu Dec 17 05:32:39 EST 2009

Hi,

As you only need to replace a single character by another single character, I can't imagine simplier than that:

DB dbtest 
DBE fld, fld2, btn
 
 
char OLDCHAR = ' '
char NEWCHAR = '|'
 
void replaceText (DBE btn)
{
  Buffer b = create 
  b = (string get fld)
  int i
  for i in 1:length b do if b[i] == OLDCHAR then set (b,i,NEWCHAR)
  set(fld, stringOf b)
  delete b 
}
 
dbtest = create("Test DB", styleCentered)
fld = field(dbtest, "Enter Text: ", "Man Woman Child", 30, false)
right(dbtest)
btn = button(dbtest, "Replace", replaceText)
 
setFocus(btn)
 
show(dbtest)


Of course, it would be more complicated if you wanted to replace one character by two, say "/" by "\/" for instance...

- Éric

Perhaps instead:

for i in 0:((length b) -1) do if...

  • Louie

Re: DB dbtest
ePiallat - Fri Dec 18 07:36:21 EST 2009

llandale - Thu Dec 17 13:22:16 EST 2009
Perhaps instead:

for i in 0:((length b) -1) do if...

  • Louie

:oops:
Yes! of course...

Re: DB dbtest
mirlo - Fri Mar 05 15:07:36 EST 2010

ePiallat - Thu Dec 17 05:32:39 EST 2009

Hi,

As you only need to replace a single character by another single character, I can't imagine simplier than that:

DB dbtest 
DBE fld, fld2, btn
 
 
char OLDCHAR = ' '
char NEWCHAR = '|'
 
void replaceText (DBE btn)
{
  Buffer b = create 
  b = (string get fld)
  int i
  for i in 1:length b do if b[i] == OLDCHAR then set (b,i,NEWCHAR)
  set(fld, stringOf b)
  delete b 
}
 
dbtest = create("Test DB", styleCentered)
fld = field(dbtest, "Enter Text: ", "Man Woman Child", 30, false)
right(dbtest)
btn = button(dbtest, "Replace", replaceText)
 
setFocus(btn)
 
show(dbtest)


Of course, it would be more complicated if you wanted to replace one character by two, say "/" by "\/" for instance...

- Éric

This seems almost like what I need, the only question is, if I want to replace a character with a string, what would the new code look like? I am trying to replace a symbol character for example with a string representing the same thing, like the º symbol being replaced with \degrees.

Re: DB dbtest
ManuelFelger - Mon Mar 08 10:12:00 EST 2010

mirlo - Fri Mar 05 15:07:36 EST 2010
This seems almost like what I need, the only question is, if I want to replace a character with a string, what would the new code look like? I am trying to replace a symbol character for example with a string representing the same thing, like the º symbol being replaced with \degrees.

//*****************************************************************************************************************************
string replaceChar(string stringToClear, string sub, string new)
{
    int offset
        int length1
        bool matchCase = true
        string s1
        string s2
 
        for (findPlainText(stringToClear, sub, offset, length1, matchCase) == true)
        {
                s1 = stringToClear[0:offset-1]
                s2 = stringToClear[offset+1:(length(stringToClear))]
 
                stringToClear = s1 new s2
        }
        return stringToClear
}
//*****************************************************************************************************************************
 
string s = "today we've got 28°"
 
 
s = replaceChar(s, "°", "degrees")
 
print s ""
 
//prints: "today we've got 28degrees"


Regards,
Manuel